#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;

/*vector<int> PrimeDecompose(unsigned num)
{
	vector<int> v;
	unsigned i = 2;

	while( num != 1)
	{
		while(num%i == 0)
		{
			v.push_back(i);
			num = num/i;
		}
		i++;
	}

	return v;
}

void PrintVector(vector<int> v)
{
	for(unsigned i = 0; i < v.size(); i++)
		cout << v[i] << "	";
	cout << endl;
}*/

int GCDWithinRange(int a, int b, int low, int high)
{
	b = __gcd(a,b);	
	
	if(low <= b && b <= high)
		return b;
	else if(b < low)
	{
		return -1;
	}
	else
	{
		if(low == high && b%low == 0)
			return low;

		int mx = -1;
		for(int i = 1; i*i <= b; i++)
		{
			if(b%i == 0)
			{
				if(low <= i && i <= high)
					mx = max(mx,i);
				if(low <= b/i && b/i <= high)
					mx = max(mx,b/i);
			}
		}

		return mx;
		/*vector<int> v;
		for(int i = 1; i*i <= b; i++)
		{
			if(b%i == 0)
			{
				v.push_back(i);
				if(i*i != b)
					v.push_back(b/i);
			}
		}

		sort(v.begin(),v.end());

		//PrintVector(v);

		vector<int>::reverse_iterator it;
		for(it = v.rbegin(); it < v.rend(); it++)
		{
			if(low <= (*it) && (*it) <= high)
				return *it;
		}*/
	}
}

int main(int argc, char* argv[])
{
	int num1, num2;
	int n;
	cin >> num1 >> num2 >> n;
	int gcd;

	//cout << "num1 = " << num1 << ", num2 = " << num2 << endl;

	while(n)
	{
		int low, high;
		cin >> low >> high;

		//cout << "low = " << low << ", high = " << high << endl;

		gcd = GCDWithinRange(num1,num2,low,high);
		cout << gcd << endl;
		n--;
	}

	return 0;
}
